/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import main.Main;

import org.apache.log4j.Logger;

/**
 * The server socket listening for connections from the forum agent site
 * 
 * 
 */
public class IntelListener implements Runnable {
   private final ExecutorService exec;

   public IntelListener() {
	  exec = Executors.newCachedThreadPool();
   }

   @Override
   public void run() {
	  ServerSocket serverSocket = null;
	  boolean listening = true;

	  try {
		 serverSocket = new ServerSocket(Main.getBotSettings().getIntelPort());
	  } catch (final IOException e) {
		 System.err.println("Could not listen on port: " + Main.getBotSettings().getIntelPort());
	  }

	  while (listening) {
		 try {
			exec.execute(new IntelHost(serverSocket.accept()));
		 } catch (final IOException e) {
			Logger.getLogger("Error").error("", e);
			break;
		 }
	  }

	  try {
		 serverSocket.close();
	  } catch (final Exception e) {
	  }
   }
}
